package map

import android.os.Bundle
import androidx.activity.viewModels
import androidx.fragment.app.viewModels
import androidx.navigation.Navigation
import com.baidu.mapapi.map.BaiduMap
import com.baidu.mapapi.map.MapView
import com.baidu.mapapi.model.LatLng
import com.zjw.zy.R
import com.zjw.zy.base.BaseActivity
import com.zjw.zy.ext.go
import com.zjw.zy.ext.longToast
import com.zjw.zy.utils.AppUtils
import com.zjw.zy.utils.taskpool.TaskPool
import map.location.LocationManager
import map.utils.MapManagerFactory
import map.utils.MapPageManager
import map.utils.MapUtils
import map.viewmodel.MapFunctionViewModel
import map.viewmodel.PreviewViewModel
import map.viewmodel.RoutePlanViewModel


/**
 * 地图 activity
 * */
class MapActivity : BaseActivity() {

    companion object {
        const val PARAMS_TARGET_LOCATION = "PARAMS_TARGET_LOCATION"
    }


    private val mapFunctionViewModel: MapFunctionViewModel by viewModels()

    private var mMapView: MapView? = null
    private var mMap: BaiduMap? = null

    override fun onBindListener() {
    }

    override fun onBindValue() {
        setFullBackGround(R.color.transparent)

        mMap = mMapView?.map
        mMap?.isMyLocationEnabled = true
        //打开室内地图
        mMap?.setIndoorEnable(true)

        intent.getParcelableExtra<LatLng>(PARAMS_TARGET_LOCATION)?.let {
            mapFunctionViewModel.emitLoveLatLng(it)
        }
    }

    override fun onFindView() {
        mMapView = findViewById(R.id.mapview)
    }

    override fun onLayoutId(): Int {
        return R.layout.map_activity
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        //地图得先前置
        MapUtils.init()
        AppUtils.addMap(this)
        super.onCreate(savedInstanceState)
        MapPageManager.init(this)
    }

    override fun onResume() {
        super.onResume()
        mMapView?.onResume()
        navigationController().navigate(R.id.nav_map_main_fragment)
    }

    override fun onPause() {
        super.onPause()
        mMapView?.onPause()
    }

    override fun onDestroy() {
        MapManagerFactory.onDestroy()
        AppUtils.removeMap()
        mMapView?.onDestroy()
        mMap?.isMyLocationEnabled = false
        mMap = null
        super.onDestroy()
    }

    override fun onBackPressed() {
        MapPageManager.getTopFragment()?.onBackClick()
    }

    override fun finish() {
        super.finish()
    }

    override fun onRestart() {
        super.onRestart()
    }

    fun navigationController() = Navigation.findNavController(this, R.id.nav_host_map)

    fun map(): BaiduMap = mMap!!
    fun mapView(): MapView = mMapView!!
}